setScreenMetrics(1080, 2400);
auto.waitFor(); //
FloatyUtil(); //=悬浮窗加载===
init();
// 滑动返回(20);
// exit();
toastLog("版本24.7.13,第一次运行前先给，后台权限，悬浮窗，后台权限，文件管理权限");
let 点爱心 = false;
let 点关注 = false;
let 公众号关注 = false;
let 视频号点赞1 = false;
let 文章阅读 = false;
let 点击确认 = true;

let 自动确定 = threads.start(function () {
  选择那些任务();
});
toastLog("5秒后自动确定");
sleep(5000);
if (点击确认) {
  findAndClick("确定");
  自动确定.interrupt();
}

let width = device.width;
let height = device.height;
let _this = this;
threads.start(function () {
  findAndClick("允许|立即开始");
});
if (!requestScreenCapture()) {
  toast("请求截图失败");
  exit();
}
sleep(3000);

打开微信页面();
showFLog("校验页面~~~");
widgetGetOne("阅读.*", 16000);
showFLog("等待登录~~~");
let 我知道了 = widgetGetOne("我知道了.1..", 8000);
if (我知道了) {
  sleep(2000);
  clickCenter(我知道了);
} else {
  findAndClick("我知道了.*");
}
findAndClick("推荐活动|互助活动");
sleep(1000);
swipe(600, 1900, 600, 1300, 600);
if (点爱心) {
  showFLog("=====爱心任务======");
  for (var i = 0; i < 10; i++) {
    if (视频点爱心()) {
      break;
    }
    sleep(3000);
  }
  showFLog("刷新后执行下一个任务");
  刷新();
}
if (点关注) {
  showFLog("=====视频号关注任务======");
  for (var i = 0; i < 6; i++) {
    if (视频号关注()) {
      break;
    }
    sleep(3000);
  }
  showFLog("刷新后执行下一个任务");
  刷新();
}
if (公众号关注) {
  showFLog("=====公众号关注推荐任务======");
  for (var i = 0; i < 9; i++) {
    if (公众号关注推荐()) {
      break;
    }
    sleep(3000);
  }
}
if (视频号点赞1) {
  showFLog("=====视频号点赞任务======");
  for (var i = 0; i < 9; i++) {
    if (视频号点赞()) {
      break;
    }
    sleep(3000);
  }
}

if (findAndClick("我的主页")) {
  刷新();
  findAndClick("点击领取");
}
if (文章阅读) {
  文章阅读任务();
}
showFLog("跑完一趟，到桌面");
sleep(1000);
back();
sleep(1000);
back();
sleep(1000);
back();

function 图片重置() {
  media.scanFile("/sdcard/atuping/weix.jpg");
  sleep(2000);
  // 创建一个Intent对象，设置其动作为扫描二维码
  var intent = new Intent();
  intent.setAction("com.tencent.mm.action.BIZSHORTCUT");
  intent.putExtra("LauncherUI.From.Scaner.Shortcut", true);
  log("截图成功");
  sleep(2000);
}

function 截图() {
  console.hide();
  sleep(1000);
  while (true) {
    if ((图 = images.captureScreen())) {
      files.create("/sdcard/atuping/");
      images.save(图, "/sdcard/atuping/weix.jpg", "jpg", 100);
      图片重置();
      sleep(500);
      return 图;
    }
  }
}

/**
 * 根据内容获取一个对象
 *
 * @param {string} contentVal
 * @param {number} timeout 默认超时时间5秒
 * @param {boolean} containType 是否带回类型
 * @param {boolean} suspendWarning 是否隐藏warning信息
 * @param {function} appendFilter 附加查询条件 详见UiSelector
 */
function widgetGetOne(contentVal, timeout, containType, suspendWarning, appendFilter, options) {
  let target = null;
  let isDesc = false;
  let waitTime = timeout || 8000;
  let timeoutFlag = true;
  showFLog(["try to find one: {} timeout: {}ms", contentVal.toString(), waitTime]);
  let checkResult = widgetCheck(contentVal, waitTime, true, appendFilter, options);
  if (!checkResult.timeout) {
    timeoutFlag = false;
    target = checkResult.target;
    isDesc = checkResult.isDesc;
  }
  // 当需要带回类型时返回对象 传递target以及是否是desc
  if (target && containType) {
    let result = {
      target: target,
      bounds: target.bounds(),
      isDesc: isDesc,
      content: isDesc ? target.desc() : target.text(),
    };
    return result;
  }
  return target;
}

/**
 * 替换控件搜索算法DFS,BFS,VDFS,VBFS,PDFS,PBFS,PVDFS,PVBFS
 * DFS,BFS为默认提供的深度优先和广度优先搜索算法
 * VDFS,VBFS为我修改的只搜索可见控件（控件包括父级不可见则直接跳过）深度优先和广度优先搜索算法 缺点是无法搜索不可见控件，适合一个界面中有巨量控件的时候用于加快搜索速度 实际数据抖音极速版从25s缩短到3s
 * PDFS,PBFS,PVDFS,PVBFS 是通过多线程搜索控件 大大加快搜索速度
 *
 * @param {string} algorithm 搜索算法DFS,BFS,VDFS,VBFS,PDFS,PBFS,PVDFS,PVBFS
 * @param {UiSelector} mselector
 * @returns
 */
function wrapSelector(algorithm, appendFilter, mselector) {
  appendFilter =
    appendFilter ||
    function (matcher) {
      return matcher;
    };
  mselector = appendFilter(mselector || selector());
  if (!algorithm) {
    return mselector;
  }
  // if (!algorithm_change_support) {
  //     log(['当前版本不支持替换搜索算法'])
  //     return mselector
  // }
  current = this.getCurrentAlgorithm();
  if (current == algorithm) {
    return mselector;
  }
  log(["替换搜索算法为：{} 原始算法：{}", algorithm, current]);
  return AlgorithmChanger.changeAlgorithm(mselector, algorithm);
}

/**
 * 查找一个子控件中的目标对象
 * @param {UiObject} container 父控件
 * @param {String} contentVal 控件文本
 * @param {number} timeout 超时时间 默认5秒
 * @param {Boolean} containType 返回结果附带文本是desc还是text
 * @param {function} appendFilter 附加查询条件 详见UiSelector
 * @param {Object} options 额外参数
 * 超时返回false
 */
function subWidgetGetOne(container, contentVal, timeout, containType, appendFilter, options) {
  options = options || {};
  timeout = timeout || 5000;
  let countDown = new java.util.concurrent.CountDownLatch(1);
  let matchRegex = new RegExp(contentVal);
  let isDesc = false;
  let isText = false;
  let target = null;
  let descThread = threads.start(function () {
    let descTarget = wrapSelector(options.algorithm, appendFilter).descMatches(matchRegex).findOneOf(container);
    if (descTarget) {
      log(["find desc {} {}", contentVal, descTarget.desc()]);
      isDesc = true;
      target = descTarget;
      countDown.countDown();
    }
  });

  let textThread = threads.start(function () {
    let textTarget = wrapSelector(options.algorithm, appendFilter).textMatches(matchRegex).findOneOf(container);
    if (textTarget) {
      log(["find text {} {}", contentVal, textTarget.text()]);
      isText = true;
      target = textTarget;
      countDown.countDown();
    }
  });

  let isTimeout = false;
  let timeoutThread = threads.start(function () {
    sleep(timeout);
    isTimeout = true;
    countDown.countDown();
  });

  countDown.await();
  descThread.interrupt();
  textThread.interrupt();
  timeoutThread.interrupt();
  if (isTimeout || (!isDesc && !isText)) {
    log("cannot find any matches " + contentVal);
  }
  if (target && containType) {
    return {
      isDesc: isDesc,
      target: target,
      bounds: target.bounds(),
      content: isDesc ? target.desc() : target.text(),
    };
  }
  return target;
}

/**
 * 查找一个子控件中的目标对象
 * @param {UiObject} container 父控件
 * @param {String} contentVal 控件id
 * @param {number} timeout 超时时间 默认5秒
 * @param {Boolean} containType 返回结果附带文本是desc还是text
 * @param {function} appendFilter 附加查询条件 详见UiSelector
 * @param {Object} options 额外参数
 * 超时返回false
 */
function subWidgetGetById(container, contentVal, timeout, containType, appendFilter, options) {
  options = options || {};
  timeout = timeout || 5000;
  let countDown = new java.util.concurrent.CountDownLatch(1);
  let matchRegex = new RegExp(contentVal);
  let isDesc = false;
  let isText = false;
  let target = null;
  let descThread = threads.start(function () {
    let descTarget = wrapSelector(options.algorithm, appendFilter).id(matchRegex).findOneOf(container);
    if (descTarget) {
      log(["find id {} {}", contentVal, descTarget.desc()]);
      isDesc = true;
      target = descTarget;
      countDown.countDown();
    }
  });

  let isTimeout = false;
  let timeoutThread = threads.start(function () {
    sleep(timeout);
    isTimeout = true;
    countDown.countDown();
  });

  countDown.await();
  descThread.interrupt();
  timeoutThread.interrupt();
  if (isTimeout || (!isDesc && !isText)) {
    log("cannot find any matches " + contentVal);
  }
  if (target && containType) {
    return {
      isDesc: isDesc,
      target: target,
      bounds: target.bounds(),
      content: isDesc ? target.desc() : target.text(),
    };
  }
  return target;
}

/**
 * 校验控件是否存在，并打印相应日志
 * @param {String} idRegex 控件文本
 * @param {String} position 日志内容 当前所在位置是否成功进入
 * @param {Number} timeoutSetting 超时时间 默认为 5秒
 */
function idWaiting(idRegex, position, timeoutSetting, appendFilter, options) {
  options = options || {};
  let waitingSuccess = idCheck(idRegex, timeoutSetting, false, appendFilter, options);
  position = position || idRegex;
  if (waitingSuccess) {
    log("等待控件成功：" + position);
    return true;
  } else {
    log("等待控件[" + position + "]失败， id：" + idRegex);
    return false;
  }
}

/**
 * id检测
 * @param {string|RegExp} idRegex
 * @param {number} timeoutSetting
 */
function idCheck(idRegex, timeoutSetting, containType, appendFilter, options) {
  options = options || {};
  let timeout = timeoutSetting || 5000;
  let timeoutFlag = true;
  let countDown = new java.util.concurrent.CountDownLatch(1);
  let target = null;
  let idCheckThread = threads.start(function () {
    target = wrapSelector(options.algorithm, appendFilter).idMatches(idRegex).findOne();
    log("find id " + idRegex);
    timeoutFlag = false;
    countDown.countDown();
  });

  let timeoutThread = threads.start(function () {
    sleep(timeout);
    countDown.countDown();
  });
  countDown.await();
  idCheckThread.interrupt();
  timeoutThread.interrupt();
  if (timeoutFlag) {
    log(["未能找到id:{}对应的控件", idRegex]);
  }
  if (containType) {
    return {
      timeout: timeoutFlag,
      target: target,
      bounds: target ? target.bounds() : null,
    };
  }
  return !timeoutFlag;
}

/**
 * 校验控件是否存在
 * @param {String} contentVal 控件文本
 * @param {Number} timeoutSetting 超时时间 单位毫秒
 * @param {Boolean} containType 返回结果附带文本是desc还是text
 * @param {Object} options 额外参数
 * 超时返回false
 */
function widgetCheck(contentVal, timeoutSetting, containType, appendFilter, options) {
  options = options || {};
  let timeout = timeoutSetting || 5000;
  let timeoutFlag = true;
  let countDown = new java.util.concurrent.CountDownLatch(1);
  let matchRegex = new RegExp(contentVal);
  let isDesc = false;
  let target = null;
  let descThread = threads.start(function () {
    target = wrapSelector(options.algorithm, appendFilter).descMatches(matchRegex).findOne();
    let res = target.desc();
    log("find desc " + contentVal + " " + res);
    timeoutFlag = false;
    isDesc = true;
    countDown.countDown();
  });

  let textThread = threads.start(function () {
    target = wrapSelector(options.algorithm, appendFilter).textMatches(matchRegex).findOne();
    let res = target.text();
    log("find text " + contentVal + "  " + res);
    timeoutFlag = false;
    countDown.countDown();
  });

  let timeoutThread = threads.start(function () {
    sleep(timeout);
    countDown.countDown();
  });
  countDown.await();
  descThread.interrupt();
  textThread.interrupt();
  timeoutThread.interrupt();
  if (timeoutFlag) {
    log("cannot find any matches " + contentVal + " timeout:" + timeout);
  }
  if (containType) {
    return {
      timeout: timeoutFlag,
      target: target,
      bounds: target ? target.bounds() : null,
      isDesc: isDesc,
    };
  }
  return !timeoutFlag;
}

/**
 * 校验控件是否存在，并打印相应日志
 * @param {String} contentVal 控件文本
 * @param {String} position 日志内容 当前所在位置是否成功进入
 * @param {Number} timeoutSetting 超时时间 单位毫秒 默认为_config.timeout_existing
 */
function widgetWaiting(contentVal, position, timeoutSetting, appendFilter, options) {
  options = options || {};
  let waitingSuccess = widgetCheck(contentVal, timeoutSetting, false, appendFilter, options);
  position = position || contentVal;
  if (waitingSuccess) {
    log("等待控件成功：" + position);
    return true;
  } else {
    log("等待控件[" + position + "]失败, 查找内容：" + contentVal);
    return false;
  }
}

/**
 * 根据id获取控件信息
 * @param {String|RegExp} idRegex id
 * @param {number} timeout 超时时间
 * @return 返回找到的控件，否则null
 */
function widgetGetById(idRegex, timeout, appendFilter, options) {
  options = options || {};
  timeout = timeout || 8000;
  let target = idCheck(idRegex, timeout, true, appendFilter, options);
  if (!target.timeout) {
    return target.target;
  } else {
    return null;
  }
}

function waitFor(action, timeout) {
  let countDown = new java.util.concurrent.CountDownLatch(1);
  let actionSuccess = false;
  let actionThread = null;
  let that = this;
  try {
    let start = new Date().getTime();
    actionThread = threads.start(function () {
      try {
        action();
        actionSuccess = true;
      } catch (e) {
        if (e.javaException instanceof com.stardust.autojs.runtime.exception.ScriptInterruptedException) {
          return;
        }
        warn("action执行异常" + e);
        that.printExceptionStack(e);
      } finally {
        countDown.countDown();
      }
      log(["action执行结束: {} 耗时：{}ms", actionSuccess, new Date().getTime() - start]);
    });
    let waitResult = countDown.await(timeout, java.util.concurrent.TimeUnit.MILLISECONDS);
    log(["waitFor方法执行完毕，action result: {}, wait result: {} cost time: {}ms", actionSuccess, waitResult, new Date().getTime() - start]);
    if (!waitResult) {
      log(["等待操作超时, 操作时间: {}ms", new Date().getTime() - start]);
    }
  } catch (e) {
    this.printExceptionStack(e);
  } finally {
    if (actionThread !== null) {
      actionThread.interrupt();
    }
  }
  return actionSuccess;
}

// let result = widgetUtils.alternativeWidget(/\s*今日签到\s*/, '.*明早7点可领.*', null, true)
// if (result.value === 1) {
/**
 * 判断控件A或者控件B是否存在；超时返回0 找到A返回1 否则返回2
 *
 * @param {string|regex} contentA 控件A的内容
 * @param {string|regex} contentB 控件B的内容
 * @param {number} timeout 超时时间
 * @param {boolean} containContent 是否传递实际内容
 * @param {function} appendFilter 附加查询条件 详见UiSelector
 * @param {object} options 额外参数
 * @return 超时返回0 找到A返回1 否则返回2
 */
function alternativeWidget(contentA, contentB, timeout, containContent, appendFilter, options) {
  options = options || {};
  timeout = timeout || _config.timeout_existing;
  let timeoutFlag = true;
  let countDown = new java.util.concurrent.CountDownLatch(1);
  let matchRegexA = new RegExp(contentA);
  let matchRegexB = new RegExp(contentB);
  let isDesc = false,
    findA = false;
  let res = null,
    target = null;
  let descThreadA = threads.start(function () {
    target = _this.wrapSelector(options.algorithm, appendFilter).descMatches(matchRegexA).findOne();
    res = target.desc();
    log("find desc " + contentA + " " + res);
    timeoutFlag = false;
    isDesc = true;
    findA = true;
    countDown.countDown();
  });

  let textThreadA = threads.start(function () {
    target = _this.wrapSelector(options.algorithm, appendFilter).textMatches(matchRegexA).findOne();
    res = target.text();
    log("find text " + contentA + "  " + res);
    timeoutFlag = false;
    findA = true;
    countDown.countDown();
  });
  let descThreadB = threads.start(function () {
    target = _this.wrapSelector(options.algorithm, appendFilter).descMatches(matchRegexB).findOne();
    res = target.desc();
    log("find desc " + contentB + " " + res);
    timeoutFlag = false;
    isDesc = true;
    countDown.countDown();
  });

  let textThreadB = threads.start(function () {
    target = _this.wrapSelector(options.algorithm, appendFilter).textMatches(matchRegexB).findOne();
    res = target.text();
    log("find text " + contentB + "  " + res);
    timeoutFlag = false;
    countDown.countDown();
  });

  let timeoutThread = threads.start(function () {
    sleep(timeout);
    countDown.countDown();
  });
  countDown.await();
  descThreadA.interrupt();
  textThreadA.interrupt();
  descThreadB.interrupt();
  textThreadB.interrupt();
  timeoutThread.interrupt();
  if (timeoutFlag) {
    log(["cannot find any matches {} or {}", contentA, contentB]);
  }
  // 超时返回0 找到A返回1 否则返回2
  let returnVal = timeoutFlag ? 0 : findA ? 1 : 2;
  if (containContent) {
    return {
      target: target,
      bounds: target ? target.bounds() : null,
      content: res,
      value: returnVal,
    };
  } else {
    return returnVal;
  }
}

// 快速打开("快手极速", "com.kuaishou.nebula", "com.yxcorp.gifshow.HomeActivity")
/**
 * 找到并且点击 如果返回错误就点击中心 返回一个布尔值
 * @param {str} 要找的字符串
 */
function findAndClick(str, sleepTime, waitSleep) {
  waitSleep = waitSleep || 300;
  sleep(waitSleep);
  sleepTime = sleepTime || 8000;
  let widgetGet = widgetGetOne(str, sleepTime);
  if (widgetGet) {
    clickView(widgetGet);
    return widgetGet;
  }
  return false;
}

function clickView(obj) {
  try {
    if (clickCenter(obj)) {
      for (let i = 0; i < 4; i++) {
        if (obj && obj.clickable) {
          if (obj.click()) {
            console.log("Control clicked successfully" + obj.text());
          }
        } else {
          console.log("Control not clickable, checking parent");
          obj = obj && obj.parent();
        }
      }
    }
  } catch (error) {
    console.error("按键点击失败：" + error);
  }
}
function clickCenter(obj) {
  console.hide();
  sleep(300);
  if (obj.bounds().centerX() > width || obj.bounds().centerY() > height || obj.bounds().centerX() < 0 || obj.bounds().centerY() < 0) {
    showFLog("不可点击范围:" + obj.bounds().centerX() + " " + obj.bounds().centerY());
    return true;
  }
  showFLog(obj.text() + " 点击位置：" + Number(obj.bounds().centerX()) + " " + Number(obj.bounds().centerY()));
  click(Number(obj.bounds().centerX()), Number(obj.bounds().centerY()));
  sleep(500);
}
// 杀死后台
function killApp(appName) {
  var name = getPackageName(appName); //通过app名称获取包名
  if (null == name) {
    console.error("没有查询douyjsb");
    return true;
  }
  app.openAppSetting(name); //通过包名打开应用的详情页(设置页)
  sleep(2000); //稍微休息一下，不然看不到运行过程，自己用时可以删除这行
  findAndClick(/强.*|停.*|结.*/);
  findAndClick(/强.*|停.*|结.*/);
  findAndClick(/确定/);
  sleep(3000);
  return false;
}

function swipex(speed) {
  let millis = parseInt(speed || 500);
  let x = parseInt(width / 2);
  let startPoint = height - 150;
  // 滑动距离，二分之一屏幕
  let distance = parseInt(height - 200);
  let endPoint = startPoint - distance;
  // 手势上划
  swipe(x, startPoint, x + 100, endPoint, millis);
}

function 滑动返回(nums) {
  let num = nums || 20;
  sleep(3000);
  for (var i = 0; i < num; i++) {
    sleep(12000);
    back();
  }
}
function 等待并返回(等待时间) {
  if (等待时间) {
    sleep(Number(等待时间.text().replace(/[^\d]/g, "")) * 1000);
    back();
    sleep(1000);
    back();
  }
}

function swipex(speed) {
  let millis = parseInt(speed || 500);
  let x = parseInt(width / 2);
  let startPoint = height - 150;
  // 滑动距离，二分之一屏幕
  // let distance = parseInt(height / 2 - 200);
  let endPoint = parseInt(height / 2 - 200);
  // 手势上划
  swipe(x, startPoint, x, endPoint, millis);
}

function showFLog(msgLog) {
  let str = msgLog + " ";
  setInfo(str);
  log(msgLog);
}
//================悬浮窗开始================
function FloatyUtil() {
  this.floatyWindow = null;
  this.floatyInitStatus = false;
  this.floatyLock = threads.lock();
  this.floatyCondition = this.floatyLock.newCondition();
}

function init() {
  if (this.floatyInitStatus) {
    return;
  }
  let _this = this;
  threads.start(function () {
    _this.floatyLock.lock();
    try {
      if (_this.floatyInitStatus) {
        return;
      }
      _this.floatyWindow = floaty.rawWindow(
        <frame gravity="left">
          <text id="content" textSize="16dp" textColor="#00ff00" bg="#000000" />
        </frame>
      );
      ui.run(function () {
        _this.floatyWindow.setTouchable(false);
        _this.floatyWindow.setPosition(50, 50);
        _this.floatyWindow.content.text("悬浮窗初始化成功");
      });
      _this.floatyInitStatus = true;
    } catch (e) {
      console.error("悬浮窗初始化失败" + e);
      _this.floatyWindow = null;
      _this.floatyInitStatus = false;
    } finally {
      _this.floatyCondition.signalAll();
      _this.floatyLock.unlock();
    }
  });
  _this.floatyLock.lock();
  try {
    if (_this.floatyInitStatus === false) {
      console.info("等待悬浮窗初始化");
      _this.floatyCondition.await();
    }
  } finally {
    _this.floatyLock.unlock();
  }
}

function setInfo(text, position, option) {
  option = option || {};
  if (this.floatyWindow === null) {
    this.init();
  }
  let _this = this;
  ui.run(function () {
    _this.floatyLock.lock();
    try {
      if (position && isFinite(position.x) && isFinite(position.y)) {
        _this.floatyWindow.setPosition(parseInt(position.x), parseInt(position.y) + 100);
      }
      if (text) {
        _this.floatyWindow.content.text(text);
      }
      if (option.textSize) {
        _this.floatyWindow.content.setTextSize(option.textSize);
      }
      if (typeof option.touchable !== "undefined") {
        _this.floatyWindow.setTouchable(option.touchable);
      }
    } finally {
      _this.floatyLock.unlock();
    }
  });
}

function 找图(str, loop) {
  if (typeof loop === "undefined") {
    loop = 3;
  }
  findImageInRegion;
  var screen = images.captureScreen();
  screen = images.cvtColor(images.grayscale(screen), "GRAY2BGRA");
  var p = findImage(screen, images.fromBase64(str), { threshold: 0.6 });
  if (p) {
    toastLog("找到啦:" + p);
    let num = p.toString().replace(/{|}/g, "");
    let nums = num.split(",");
    click((nums[0] * 10) / 10, (nums[1] * 10) / 10);
    log("点击坐标：" + (nums[0] * 10) / 10 + " " + (nums[1] * 10) / 10);
    return true;
  } else if (loop-- > 1) {
    sleep(500);
    log("没查询到开始倒数第: b  " + loop + " 找图");
    return 找图(str, loop);
  } else {
    toastLog("没找到，找图失败");
    return false;
  }
}

function 开始任务() {
  let 有吗 = findAndClick("开始任务|.*100.");
  if (有吗 && 有吗.text() == "开始任务") {
    if (findAndClick("允许|打开看看|打开|打开App", 8000)) {
      findAndClick("允许|打开");
    }
    return true;
  }
}

function 视频点爱心() {
  let 视频号点爱心 = widgetGetOne("视频点爱心推荐");
  if (视频号点爱心) {
    if (视频号点爱心.parent().child(6).text() == "开始活动") {
      视频号点爱心.parent().child(6).click();
    } else {
      return true;
    }

    if (长按识别()) {
      return true;
    }

    //点击爱心
    const 微信爱心图片 =
      "data:image/png;base64," +
      "iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAA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fbu3avjzzzP2bdvHzdu3GBlZeX/HtOQnpNDIVEUdV0zGo14+OGHefnll/UMRTzOSy+9xNLSknoiGavWdU2r1dp8hgZYFMkgMCylJg+0dLtdfZJBSErOvs+cOcPCwoKqVIBdu3YxGAy4cuWKblyeJZGMy0LkIOnxxx/XwyXhtU8++YQjR46o+pYxkpCkZVmqqK1Op7M4mUy2SA35XYhJ9JLAn5AmwM2bN7lw4QJ79uyh0+moPrr33nvp9Xqsra3ppFB4RGSQRH1ubo4f//jHn82oPs3+iy++yLVr10iSBM/z9HhbKmjaEltFUSzKBHD6kF6O1KQcxNc3m036/f4WiL516xbnzp1j9+7danbqumb37t18/PHHLC0tKbxPD8Udx+HRRx/lxIkTVFWl9mEymfDUU09x6dIlLUWpCDF4AseyXsvzvEUZtYgzExMjFxW9JE5vWgEIIvV6Pf7+97/z6KOPKtJlWcbOnTt10i+ZlhHP3Nwcx44dU5foOA7D4ZBjx47x4YcfKpPLpH56qimfl2BbhmEsOo6jjyYJp0j5yOBsOBxuOZkS4yOPRLmuS7/f5/z58zz00EM6RKiqioWFBT766CPW1tY0EI899hjPPvvs5tnGp3oqyzIOHz7MjRs3dBL/vwglz624rrvlUaj/Ar4oMG+03sJ6AAAAAElFTkSuQmCC";
    随机等待();
    找图(微信爱心图片);
    sleep(4000);
    back();
    sleep(1000);
    if (!findAndClick("点红心成功")) {
      back();
      showFLog("点赞拦截退出");
      findAndClick("拦截了.*");
      sleep(3000);
      // back();
      return true;
    }
    sleep(3000);
  }
}

function 视频号关注() {
  let 视频号点爱心 = widgetGetOne("视频号关注推荐");
  if (视频号点爱心) {
    if (视频号点爱心.parent().child(6).text() == "开始活动") {
      视频号点爱心.parent().child(6).click();
    } else {
      return true;
    }
    if (长按识别()) {
      return true;
    }
    随机等待();
    if (widgetGetOne("没有更多.*")) {
      showFLog("系统拉黑~不关注");
      back();
      findAndClick(".*未关注");
      return true;
    }
    findAndClick("关注");
    sleep(4000);
    back();
    sleep(1000);
    if (!findAndClick(".*已关注")) {
      showFLog("视频号关注失败");
      back();
      if (widgetGetOne(".*已关注")) {
        back();
        findAndClick(".*已关注");
        return false;
      }
      findAndClick(".*未关注");
      return true;
    }
    sleep(3000);
  }
}

function 公众号关注推荐() {
  let 视频号点爱心 = widgetGetOne("公众号关注推荐");
  if (视频号点爱心 && 视频号点爱心.parent().child(6).text() == "开始活动") {
    视频号点爱心.parent().child(6).click();
  } else {
    return true;
  }
  if (长按识别()) {
    return true;
  }
  随机等待();
  findAndClick("关注公众号");
  sleep(4000);
  let 已关注 = widgetGetOne("关注|已关注|已关注公众号|发消息");
  if (已关注 && (已关注.text() == "已关注" || 已关注.text() == "发消息")) {
    back();
    findAndClick(".*已关注", 8000, 2000);
  } else {
    back();
    sleep(3000);
    let 已关注 = widgetGetOne("关注|已关注|已关注公众号|发消息");
    if (已关注 && (已关注.text() == "已关注" || 已关注.text() == "发消息" || 已关注.text() == "已关注公众号")) {
      back();
      findAndClick(".*已关注", 8000, 2000);
    } else {
      showFLog("关注失败");
      back();
      sleep(3000);
      findAndClick(".*未关注");
      return true;
    }
  }
  sleep(3000);
}

function 视频号点赞() {
  let 视频号点爱心 = widgetGetOne("视频号大拇指");
  if (视频号点爱心 && 视频号点爱心.parent().child(6).text() == "开始活动") {
    视频号点爱心.parent().child(6).click();
  } else {
    return true;
  }
  sleep(3000);
  if (长按识别()) {
    return true;
  }
  随机等待();
  const 微信爱心图片 =
    "";

  随机等待();
  找图(微信爱心图片);
  sleep(4000);
  back();
  sleep(1000);
  if (!findAndClick(".*成功")) {
    back();
    showFLog("点赞拦截退出");
    findAndClick("拦截了.*");
    sleep(3000);
    return true;
  }
  sleep(3000);
}
function idAndClick(str, sleepTime) {
  sleepTime = sleepTime || 8000;
  let widgetGet = widgetGetById(str, sleepTime);
  if (widgetGet) {
    clickView(widgetGet);
    return widgetGet;
  }
  return false;
}
function 打开微信页面() {
  showFLog("打开微信");
  app.startActivity({
    className: "com.tencent.mm.ui.LauncherUI",
    packageName: "com.tencent.mm",
  });
  widgetGetOne("我");
  if (!idAndClick("com.tencent.mm:id/h6y")) {
    log("没查询到首页控件，返回2次");
    back();
    if (!widgetGetOne("我", 5000)) {
      back();
      if (!widgetGetOne("我", 5000)) {
        back();
      }
    }
    idAndClick("com.tencent.mm:id/h6y");
  }
  findAndClick("文件传输助手");
  input("<a href='http://91700528192.tt.bendishenghuochwl1.cn/pages/app/daily/daily?user_id=259381' >美添赚</a>");
  sleep(1000);
  findAndClick("发送");
  sleep(1000);
  findAndClick("美添赚");
}

function 长按识别() {
  let 逛领券 = widgetGetOne(".*成功率.*|长按识别.*");
  if (逛领券) {
    showFLog(逛领券.text());
    sleep(2000);
  } else {
    showFLog("没查询到成功率，退出");
    return true;
  }
  showFLog("长按视频号图片");
  press(device.width / 2, 逛领券.bounds().top - 200, 1000);
  sleep(3000);
  let 识别图中的二维码 = findAndClick("识别图中的二维码|前往图中.*");
  if (识别图中的二维码 && /图/.test(识别图中的二维码.text())) {
    showFLog("等待加载~");
    sleep(5000);
  } else {
    findAndClick("取消");
    showFLog("没有查询到识别图中二维码，退出");
    return true;
  }
}

function 刷新() {
  findAndClick("更多信息");
  findAndClick("刷新", 8000, 2000);
  sleep(2000);
}

function 随机等待() {
  showFLog("随机等待10-15秒");
  sleep(random(10000, 15000));
}

function 选择那些任务() {
  let 选择的任务 = [];
  let storage = storages.create("1934017003@qq.com");
  if (storage.get("任务-美添赚")) {
    选择的任务 = storage.get("任务-美添赚");
    log(选择的任务);
  } else {
    选择的任务 = [0, 1, 2, 3];
  }
  dialogs.multiChoice(
    "5s后自动运行",
    ["视频点爱心", "视频号关注任务", "公众号关注", "视频大拇指", "文章阅读"],
    选择的任务,
    function (selectedIndices) {
      console.log("用户选择了以下选项：");
      选择的任务 = [];
      selectedIndices.forEach(function (i) {
        选择的任务.push(i);
        log(selectedIndices[i]);
      });
    }
  );
  if (选择的任务.length > 0) {
    storage.put("任务-美添赚", 选择的任务);
    选择的任务.forEach((v) => {
      if (v == 0) 点爱心 = true;
      if (v == 1) 点关注 = true;
      if (v == 2) 公众号关注 = true;
      if (v == 3) 视频号点赞1 = true;
      if (v == 4) 文章阅读 = true;
      点击确认 = false;
    });
  } else {
    showFLog("一个都没选择 退出脚本");
    exit();
  }
}
function 文章阅读任务() {
  toastLog("没做");
}
